package com.thfw.test.view.fragment;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.thfw.wmd.R;
import com.thfw.test.model.authManage.AuthBaseBean;
import com.thfw.test.model.authManage.LampInterfaceBean;
import com.thfw.test.model.bean.CameraBean;
import com.thfw.test.model.bean.GatewayBean;
import com.thfw.test.model.bean.LampListBean;
import com.thfw.test.model.bean.RegionBean;
import com.thfw.test.utils.ConfigurationEntry;
import com.thfw.test.utils.StringUtils;
import com.thfw.test.view.adapter.ExpandableListviewAdapter;
import com.thfw.test.view.network.AuthManageService;
import com.thfw.test.view.widget.TouchProgessView;

import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.HashMap;

import butterknife.BindView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import scooper.cn.sc_base.log.SCLog;
import utils.RetrofitWrapper;

import static com.thfw.test.model.bean.RegionBean.prinfAllRegion;

/**
 * Created BY Cuishikun
 * Created ON 2021/11/12
 * Package Name com.thfw.test.view.fragment
 **/
public class DeviceManagementFragment extends BaseFragment {

    @BindView(R.id.listview)
    ExpandableListView expand_list_id;
    ExpandableListviewAdapter adapter;
    int port;
    String host;

    private String[] groups = {"工矿灯", "摄像头", "网关"};

    private static final String TAG = DeviceManagementFragment.class.getCanonicalName();
    //注意，字符数组不要写成{{"A1,A2,A3,A4"}, {"B1,B2,B3,B4，B5"}, {"C1,C2,C3,C4"}}
    private HashMap<String, ArrayList<Object>> childs = new HashMap<>();
    private ArrayList<LampListBean> lampList;
    private ArrayList<CameraBean> cameraList;
    private ArrayList<GatewayBean> gatewayList;

    public static DeviceManagementFragment newInstance() {
        DeviceManagementFragment fragment = new DeviceManagementFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.fm_devicemanagement;
    }

    @Override
    protected void initView() {

        host = getConfig().getString(ConfigurationEntry.HOST, "192.168.161.121");
        port = getConfig().getInt(ConfigurationEntry.PORT, 8080);
//        initAdapter();

        getLampList();
    }

    @Override
    protected void firstCreate() {

    }


    public void initAdapter() {

        adapter = new ExpandableListviewAdapter(getActivity(), groups, childs);
        expand_list_id.setAdapter(adapter);
        //默认展开第一个数组
        expand_list_id.expandGroup(0);
        //关闭数组某个数组，可以通过该属性来实现全部展开和只展开一个列表功能
        //expand_list_id.collapseGroup(0);
        expand_list_id.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView expandableListView, View view, int groupPosition, long l) {
//                showToastShort(groups[groupPosition]);
                return false;
            }
        });
        //子视图的点击事件
        expand_list_id.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView expandableListView, View view, int groupPosition, int childPosition, long l) {
//                showToastShort(childs[groupPosition][childPosition]);
                switch (groupPosition) {
                    case 0:
                        break;
                    case 1:
                        break;
                    case 2:
                        break;
                }
                return true;
            }
        });
    }


    //区域选择提示

    private void getLampList() {
        try {
            String url = StringUtils.buildURL("http", host, port, "", null).toString();
            String token = getConfig().getString(ConfigurationEntry.TOKEN, "");

            RetrofitWrapper.getInstance(getActivity(), url).create(AuthManageService.class).getLampList(token).enqueue(new Callback<LampInterfaceBean<ArrayList<LampListBean>>>() {
                @Override
                public void onResponse(Call<LampInterfaceBean<ArrayList<LampListBean>>> call, Response<LampInterfaceBean<ArrayList<LampListBean>>> response) {
                    SCLog.e(TAG, "response is onResponse" + call.toString());
                    if(response!=null && response.body()!=null && response.body().getRows()!=null){
                        lampList = response.body().getRows();
                        ArrayList<Object> list = new ArrayList<>();
                        for (LampListBean bean : response.body().getRows()) {
                            list.add(bean);
                        }
                        childs.put(groups[0], list);
                        getCameraList();
                    }else{
                        Toast.makeText(getActivity(), response.message(), Toast.LENGTH_SHORT).show();
                    }

                }

                @Override
                public void onFailure(Call<LampInterfaceBean<ArrayList<LampListBean>>> call, Throwable t) {
                    SCLog.e(TAG, "response is onFailure" + call.toString());

                }
            });
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }


    //s摄像头

    private void getCameraList() {
        try {
            String url = StringUtils.buildURL("http", host, port, "", null).toString();
            String token = getConfig().getString(ConfigurationEntry.TOKEN, "");

            RetrofitWrapper.getInstance(getActivity(), url).create(AuthManageService.class).getCameraList(token).enqueue(new Callback<LampInterfaceBean<ArrayList<CameraBean>>>() {
                @Override
                public void onResponse(Call<LampInterfaceBean<ArrayList<CameraBean>>> call, Response<LampInterfaceBean<ArrayList<CameraBean>>> response) {
                    SCLog.e(TAG, "response is onResponse" + call.toString());
                    if(response!=null && response.body()!=null && response.body().getRows()!=null) {
                        cameraList = response.body().getRows();
                        ArrayList<Object> list = new ArrayList<>();
                        for (CameraBean bean : response.body().getRows()) {
                            list.add(bean);
                        }
                        childs.put(groups[1], list);
                        getGatewayList();
                    }else{
                        Toast.makeText(getActivity(), response.message(), Toast.LENGTH_SHORT).show();
                    }
                }

                @Override
                public void onFailure(Call<LampInterfaceBean<ArrayList<CameraBean>>> call, Throwable t) {
                    SCLog.e(TAG, "response is onFailure" + call.toString());

                }
            });
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }


    //区域选择提示

    private void getGatewayList() {
        try {
            String url = StringUtils.buildURL("http", host, port, "", null).toString();
            String token = getConfig().getString(ConfigurationEntry.TOKEN, "");

            RetrofitWrapper.getInstance(getActivity(), url).create(AuthManageService.class).getGatewayList(token).enqueue(new Callback<LampInterfaceBean<ArrayList<GatewayBean>>>() {
                @Override
                public void onResponse(Call<LampInterfaceBean<ArrayList<GatewayBean>>> call, Response<LampInterfaceBean<ArrayList<GatewayBean>>> response) {
                    SCLog.e(TAG, "response is onResponse" + call.toString());

                    if(response!=null && response.body()!=null && response.body().getRows()!=null) {
                        gatewayList = response.body().getRows();
                        ArrayList<Object> list = new ArrayList<>();
                        for (GatewayBean bean : response.body().getRows()) {
                            list.add(bean);
                        }
                        childs.put(groups[2], list);
                        initAdapter();
                    }else{
                        Toast.makeText(getActivity(), response.message(), Toast.LENGTH_SHORT).show();
                    }


                }

                @Override
                public void onFailure(Call<LampInterfaceBean<ArrayList<GatewayBean>>> call, Throwable t) {
                    SCLog.e(TAG, "response is onFailure" + call.toString());

                }
            });
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }

}
